package com.ruoyi.blog.controller;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.blog.domain.AcLike;
import com.ruoyi.blog.service.IAcBlogService;
import com.ruoyi.blog.service.IAcLikeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.blog.domain.AcComment;
import com.ruoyi.blog.service.IAcCommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论管理Controller
 * 
 * @author ruoyi
 * @date 2022-06-19
 */
@RestController
@RequestMapping("/blog/comment")
public class AcCommentController extends BaseController
{
    @Autowired
    private IAcCommentService acCommentService;

    @Autowired
    private IAcBlogService acBlogService;

    @Autowired
    private IAcLikeService acLikeService;

    @GetMapping("/getCommentList")
    public TableDataInfo getCommentList(Long blogId)
    {
        AcComment acComment = new AcComment();
        acComment.setBlogId(blogId);
        List<AcComment> list = acCommentService.selectAcCommentList(acComment);
        for (AcComment comment : list) {
            AcLike like = new AcLike();
            like.setObjType(1l);
            like.setObjId(comment.getId());

            like.setIsLike(1);
            like.setUserId(getUserId());
            comment.setUp(acLikeService.selectAcLikeList(like).size() > 0);
            like.setUserId(null);
            comment.setUpCount(acLikeService.selectAcLikeList(like).size());

            like.setIsLike(0);
            like.setUserId(getUserId());
            comment.setDown(acLikeService.selectAcLikeList(like).size() > 0);
            like.setUserId(null);
            comment.setDownCount(acLikeService.selectAcLikeList(like).size());
        }
        Collections.sort(list, (o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
        return getDataTable(list);
    }

    @PostMapping("/addComment")
    public AjaxResult addComment(@RequestBody AcComment acComment)
    {
        acComment.setUserId(getUserId());
        if (acBlogService.selectAcBlogById(acComment.getBlogId()) == null)
            return AjaxResult.error("博客已删除或不存在");
        return toAjax(acCommentService.insertAcComment(acComment));
    }

    /**
     * 查询评论管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcComment acComment)
    {
        startPage();
        List<AcComment> list = acCommentService.selectAcCommentList(acComment);
        return getDataTable(list);
    }

    /**
     * 导出评论管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:export')")
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcComment acComment)
    {
        List<AcComment> list = acCommentService.selectAcCommentList(acComment);
        ExcelUtil<AcComment> util = new ExcelUtil<AcComment>(AcComment.class);
        util.exportExcel(response, list, "评论管理数据");
    }

    /**
     * 获取评论管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(acCommentService.selectAcCommentById(id));
    }

    /**
     * 新增评论管理
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:add')")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcComment acComment)
    {
        return toAjax(acCommentService.insertAcComment(acComment));
    }

    /**
     * 修改评论管理
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:edit')")
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcComment acComment)
    {
        return toAjax(acCommentService.updateAcComment(acComment));
    }

    /**
     * 删除评论管理
     */
    @PreAuthorize("@ss.hasPermi('blog:comment:remove')")
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acCommentService.deleteAcCommentByIds(ids));
    }
}
